package com.example.myapplication;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.AppCompatEditText;
import androidx.appcompat.widget.AppCompatTextView;
import androidx.appcompat.widget.LinearLayoutCompat;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.google.gson.Gson;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;

import java.util.ArrayList;
import java.util.Date;

@SuppressWarnings("ALL")
public class MainActivity extends AppCompatActivity {

    private final ArrayList<SevendaysBase> arrayList = new ArrayList<>();
    private final MainAdapter adapter = new MainAdapter(arrayList);
    private final String[] ips = {"139.203.134.244", "223.104.90.8", "112.10.87.52", "122.238.62.125", "36.48.78.60",
            "112.97.213.185", "125.66.79.120", "116.232.64.29", "115.193.181.151", "221.192.178.10", "113.109.118.151",
            "101.93.65.57", "60.23.118.129", "8.135.18.242", "1.189.204.105"};
    private RecyclerView mainRecycleView;
    private AppCompatTextView queryTextview;

    private AppCompatTextView defaultTextView;
    private AppCompatEditText et_box;
    private LinearLayout skylinearLayout;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //实例化控件
        setContentView(R.layout.activity_main);
        mainRecycleView = findViewById(R.id.mainRecycleView);
        queryTextview = findViewById(R.id.queryTextview);
        defaultTextView = findViewById(R.id.defaultTextView);
        et_box = findViewById(R.id.et_box);
        skylinearLayout = findViewById(R.id.skylinearLayout);

        //加载默认IP地址的信息
        csh();
        initialize();
        //监听文本框内容为空显示默认IP事件
        initListener();


        queryTextview.setOnClickListener(v -> {


        });



//        OkGo.<String>get("https://tianqiapi.com/api?version=v1&appid=42187322&appsecret=435SbMQf&city=玉林").execute(new StringCallback() {
//            @Override
//            public void onSuccess(Response<String> response) {
//                SevendaysBase.DataDTO sevendaysBase = new Gson().fromJson(response.body(), SevendaysBase.DataDTO.class);
//
//
//            }
//        });
//

        for (int i = 0; i < ips.length; i++) {
            SevendaysBase.DataDTO sevendaysBase = new SevendaysBase.DataDTO();
            sevendaysBase.getDay();
         //   arrayList.add(adapter);
        }

        SevendaysBase sevendaysBase = new SevendaysBase();
        arrayList.add(sevendaysBase);
        mainRecycleView.setLayoutManager(new LinearLayoutManager(this));
        mainRecycleView.setAdapter(adapter);


        //初始化事件
//        adapter.setOnItemClickListener(new MainAdapter.OnItemClickListener() {
//            @Override
//            public void onClick(int i, IpBean ipBean) {
//                arrayList.get(i).s("查询中...");
//                adapter.notifyDataSetChanged();
//                OkGo.<String>get("http://whois.pconline.com.cn/ipJson.jsp?ip=" + ipBean.getIp() + "&json=true").execute(new StringCallback() {
//                    @Override
//                    public void onSuccess(Response<String> response) {
//                        IpBean ipBean1 = new Gson().fromJson(response.body(), IpBean.class);
//                        arrayList.remove(i);
//
//                        adapter.notifyDataSetChanged();
//                    }
//                });
//
//            }
//
//            @Override
//            public void onClick1(int i, IpBean ipBean) {
//
//                Intent intent = new Intent(MainActivity.this, DetailActivity.class);
//                intent.putExtra("ip", ipBean.getIp());
//                startActivity(intent);
//
////22
//
//
//            }
//        });
//        queryTextview.setOnClickListener(v -> {
//            for (int i = 0; i < arrayList.size(); i++) {
//                final int position = i;
//                arrayList.get(position).setAddr("查询中...");
//                adapter.notifyDataSetChanged();
//                OkGo.<String>get("http://whois.pconline.com.cn/ipJson.jsp?ip=" + arrayList.get(i).getIp() + "&json=true").execute(new StringCallback() {
//                    @Override
//                    public void onSuccess(Response<String> response) {
//                        IpBean ipBean1 = new Gson().fromJson(response.body(), IpBean.class);
//                        arrayList.remove(position);
//                        arrayList.add(position, ipBean1);
//                        adapter.notifyDataSetChanged();
//                    }
//                });
//
//            }
//
//        });


    }

    private void initialize() {


    }


    private void csh() {

        if (et_box.length() < 1) {
            mainRecycleView.setVisibility(View.GONE);

        }
        ;

        OkGo.<String>get("https://tianqiapi.com/api?version=v6&appid=44224334&appsecret=aZCdyiv3").execute(new StringCallback() {
            @Override
            public void onSuccess(Response<String> response) {
                WeatherBean weatherBean = new Gson().fromJson(response.body(), WeatherBean.class);
                defaultTextView.setText("城市：");
                defaultTextView.append(weatherBean.getCity() + weatherBean.getCityEn());
                defaultTextView.append("\n天气：");
                defaultTextView.append(weatherBean.getTem2() + "°C" + "~" + weatherBean.getTem1() + "°C" + weatherBean.getWea());
                defaultTextView.append("\n风向：");
                defaultTextView.append(weatherBean.getWin() + " . " + weatherBean.getWinSpeed() + " . " + weatherBean.getWinMeter());
                defaultTextView.append("\n空气：");
                defaultTextView.append(weatherBean.getAirLevel() + " . PM2.5的值为 " + weatherBean.getAirPm25());
                defaultTextView.append("\n提示：");
                defaultTextView.append(weatherBean.getAirTips() + "");
            }
        });

    }


    private TextWatcher textWatcher = new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

        }

        @Override
        public void afterTextChanged(Editable s) {
            if (et_box.length() < 1) {
                mainRecycleView.setVisibility(View.GONE);
                skylinearLayout.setVisibility(View.VISIBLE);
            } else {
                mainRecycleView.setVisibility(View.VISIBLE);
                skylinearLayout.setVisibility(View.GONE);

            }

        }
    };


    private void initListener() {
        et_box.addTextChangedListener(textWatcher);
    }


}
